begin
  # In case you use Gosu via rubygems.
  require 'rubygems'
rescue LoadError
  # In case you don't.
end

require 'gosu'
include Gosu

# PlaceGame for the window object
window = ""
$screen_width = 2560
$screen_height = 960

class Game < Window
  attr_reader :map
  SCREEN_WIDTH = 2560
  SCREEN_HEIGHT = 1204
  def initialize
    super(SCREEN_WIDTH, SCREEN_HEIGHT, true)
    self.caption = "test"
    @tileset = Image.load_tiles(self, "media/tileset.png", 60, 60, true)
    @i = 1
    @img = Gosu::Image.new(self, "media/tank.png")
    puts @tileset[0].width
    @font = Gosu::Font.new(self, Gosu::default_font_name, 16)
  end
  def update
    @i+=1
  end
  def draw
    m = 1
    @tileset[0].draw_as_quad(0, 0, 0xffffffff, 60 * m, 0, 0xffffffff, 0, 60 * m, 0xffffffff, 60 * m, 60 * m, 0xffffffff, 0)
    @tileset[0].draw_as_quad(50 * m, 0, 0xffffffff, 110 * m, 0, 0xffffffff, 50 * m, 60 * m, 0xffffffff, 110 * m, 60 * m, 0xffffffff, 0)
    #@tileset[0].draw_as_quad(0, 0, 0xff00ffff, 60, 0, 0xffff00ff, 0, 60, 0xffffff00, 60, 60, 0x88ffffff, 0)
    #@tileset[0].draw(0, 0, 0)
    draw_circle(100,100, 50, 0xffffff00)
    c = Gosu::Color.new(128, 128, 128, 128)
    draw_octagon(200,500, @i, c)
    draw_quad(300, 300, 0xffffffff, 400, 300, 0xffffffff, 300, 400, 0xffffffff, 400, 400, 0xffffffff, 0)
    (0..1000).each do |x|
      draw_point(x*3, 500 + Math.tan(x.to_f/100) * 10)
    end
    @img.draw(1500,500,1)
    
    @font.draw_rel("test", self.width, 16, 999, 1, 0)
  end
  
  def button_down(id)
    if id == Button::KbEscape then close end
  end
  
  def draw_point(x, y, size = 2)
    size = size / 2
    draw_quad(x-size, y-size, 0xffffffff, x+size, y-size, 0xffffffff, x-size, y+size, 0xffffffff, x+size, y+size, 0xffffffff, 0)
  end
  
  def draw_circle(centerx, centery, radius, color)
    (radius*2).times do |y|
      x1 = centerx - Math.sqrt(radius ** 2 - (radius - y) ** 2)
      x2 = centerx + Math.sqrt(radius ** 2 - (radius - y) ** 2)
      draw_line(x1, y + centery, color, x2, y + centery, color, 0)
    end
  end
  
  def draw_octagon(center_x, center_y, radius, color = 0xffffffff)
    side = radius * 0.707
    draw_quad(center_x - side/2, SCREEN_HEIGHT - (center_y + radius), color,
              center_x + side/2, SCREEN_HEIGHT - (center_y + radius), color,
              center_x - side/2, SCREEN_HEIGHT - (center_y - radius), color,
              center_x + side/2, SCREEN_HEIGHT - (center_y - radius), color)
    draw_quad(center_x - radius, SCREEN_HEIGHT - (center_y + side/2), color,
              center_x + radius, SCREEN_HEIGHT - (center_y + side/2), color,
              center_x - radius, SCREEN_HEIGHT - (center_y - side/2), color,
              center_x + radius, SCREEN_HEIGHT - (center_y - side/2), color)
    draw_triangle(center_x - side/2, SCREEN_HEIGHT - (center_y + radius), color,
              center_x - radius, SCREEN_HEIGHT - (center_y + side/2), color,
              center_x - side/2, SCREEN_HEIGHT - (center_y + side/2), color)
    draw_triangle(center_x + side/2, SCREEN_HEIGHT - (center_y + radius), color,
              center_x + radius, SCREEN_HEIGHT - (center_y + side/2), color,
              center_x + side/2, SCREEN_HEIGHT - (center_y + side/2), color)
    draw_triangle(center_x - side/2, SCREEN_HEIGHT - (center_y - radius), color,
              center_x - radius, SCREEN_HEIGHT - (center_y - side/2), color,
              center_x - side/2, SCREEN_HEIGHT - (center_y - side/2), color)
    draw_triangle(center_x + side/2, SCREEN_HEIGHT - (center_y - radius), color,
              center_x + radius, SCREEN_HEIGHT - (center_y - side/2), color,
              center_x + side/2, SCREEN_HEIGHT - (center_y - side/2), color)
  end
  
end

Game.new.show
